//
//  HORepairerDetailViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 7/5/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HORepairerDetailViewController.h"
#import "HORepairerBaseInfoTableViewCell.h"
#import "HOAppraiserTableViewCell.h"
#import "HOImageMgr.h"
#import "UIImageView+WebCache.h"
#import "HOConstants.h"
#import "RatingBar.h"
#import "HOUtils.h"
#import "HOAppraiseListViewController.h"

@interface HORepairerDetailViewController ()

@property(nonatomic, retain)HORepairerEntity *entity;
@end

@implementation HORepairerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	self.entity = [[[HORepairerEntity alloc] init] autorelease];
	[self requestData];
}

- (void)requestData
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetRepairmanInfo,
								  kHO_FIELD_UserId:[NSNumber numberWithInt:self.userId]}];
}
- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"维修人资料", @"维修人资料")];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
		return 75;
	if(indexPath.section == 1)
	{
		HORepairerBaseInfoTableViewCell *cell = (HORepairerBaseInfoTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
		[cell setEntity:self.entity];
		return [cell getCellHeight];
	}
	else if(indexPath.section == 2)
	{
		if(self.entity.appraiseEntity == nil)
			return 1;
		HOAppraiserTableViewCell *cell = (HOAppraiserTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
		[cell setEntity:self.entity.appraiseEntity];
		return [cell getCellHeight];
	}
    return 64;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	if(self.entity.appraiseEntity == nil)
		return 2;
    return 3;
}


- (UIView *)getFooterView
{
	UIView *view = [[[UIView alloc]
					initWithFrame:CGRectMake(0,
											 0,
											 self.view.frame.size.width,
											 110)] autorelease];
	
	UILabel *titleLbl = [[[UILabel alloc]
					 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											 3,
											self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
											  22)] autorelease];

	[view addSubview:titleLbl];
	titleLbl.textColor = TEXT_COLOR_666666;
	titleLbl.font = [UIFont systemFontOfSize:13];
	titleLbl.text = NSLocalizedString(@"客户评价", @"客户评价");

	UIImage *img = [UIImage imageNamed:@"baoxiuxiangqing_kongbaiye"];
	UIImageView *imgView = [[[UIImageView alloc]
				initWithFrame:CGRectMake((self.view.frame.size.width - img.size.width) / 2,
										 32,
										 img.size.width,
										 img.size.height)] autorelease];
	imgView.image = img;
	[view addSubview:imgView];
	
	UILabel *lbl = [[[UILabel alloc]
			initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
									 HO_HORIZONAL_PADDING + imgView.frame.size.height + HO_HORIZONAL_PADDING,
									 self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
									 22)] autorelease];
	lbl.tag = 143;
	[view addSubview:lbl];
	lbl.textAlignment = NSTextAlignmentCenter;
	lbl.textColor = TEXT_COLOR_999999;
	lbl.font = [UIFont systemFontOfSize:13];
	lbl.text = NSLocalizedString(@"当前没有任何客户评价", @"当前没有任何客户评价");
	return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return [HOUtils defaultOnePixelConversion];
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
	UIView *view = [[[UIView alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  tableView.frame.size.width,
											  height)] autorelease];
	
	UILabel *lbl = [[UILabel alloc]
					 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											  (height - 22) / 2,
											  tableView.frame.size.width - HO_HORIZONAL_PADDING * 2,
											  22)];
	lbl.textColor = TEXT_COLOR_666666;
	lbl.font = [UIFont systemFontOfSize:13];
	if(section == 1)
	{
		lbl.text = NSLocalizedString(@"基本信息", @"基本信息");
	}
	else if(section == 2)
	{
		lbl.text = NSLocalizedString(@"客户评价", @"客户评价");
	}
	[view addSubview:lbl];
	[lbl release];
	return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
	if(section == 2)
		return 32;
	return [HOUtils defaultOnePixelConversion];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
	CGFloat height = [self tableView:tableView heightForFooterInSection:section];
	UIView *view = [[[UIView alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  tableView.frame.size.width,
											  height)] autorelease];
	if(section == 2 && self.entity.appraiseEntity)
	{
		UIButton *btn = [[UIButton alloc]
					  initWithFrame:CGRectMake(0,
											   0,
											   tableView.frame.size.width,
											   32)];
		[btn addTarget:self action:@selector(didSelectMore) forControlEvents:UIControlEventTouchUpInside];
		[btn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xf5f5f5) forView:btn] forState:UIControlStateNormal];
		[btn setTitle:NSLocalizedString(@"查看更多>>", @"查看更多>>") forState:UIControlStateNormal];
		[btn setTitleColor:MAIN_COLOR_NORMAL forState:UIControlStateNormal];
		btn.titleLabel.font = [UIFont systemFontOfSize:13];
		[view addSubview:btn];
		[btn release];
	}
	return view;
}

- (void)didSelectMore
{
	HOAppraiseListViewController *viewCtrl = [[HOAppraiseListViewController alloc] init];
	viewCtrl.userId = self.entity.userId;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
	{
		CGFloat height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		static NSString *UIRepairerTableViewCell = @"UIRepairerTableViewCell";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:UIRepairerTableViewCell];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: UIRepairerTableViewCell] autorelease];
		}
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		UIImageView *headerImgView = (UIImageView *)[cell viewWithTag:112];
		if(headerImgView == nil)
		{
			headerImgView = [[[UIImageView alloc]
							  initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
													   HO_HORIZONAL_PADDING,
													   44,
													   44)] autorelease];
			headerImgView.tag = 112;
			[headerImgView.layer setCornerRadius:22];
			[cell.contentView addSubview:headerImgView];
			[headerImgView.layer setMasksToBounds:YES];
		}
		[headerImgView sd_setImageWithURL:[NSURL URLWithString:self.entity.headPic]
						 placeholderImage:[[HOImageMgr sharedInstance] getDefaultImage]];
		
		
		UILabel *workStatusLbl = (UILabel *)[cell viewWithTag:1123];
		if(workStatusLbl == nil)
		{
			workStatusLbl = [[[UILabel alloc]
							  initWithFrame:CGRectMake(16,
													   50,
													   40,
													   22)] autorelease];
			workStatusLbl.tag = 1123;
			[cell.contentView addSubview:workStatusLbl];
			workStatusLbl.textColor = TEXT_COLOR_181818;
			workStatusLbl.font = [UIFont systemFontOfSize:12];
		}
		if(self.entity.isWorking)
		{
			workStatusLbl.text = @"[在线]";
			workStatusLbl.textColor = UIColorFromRGB(0x0b7f1e);
		}
		else
		{
			workStatusLbl.text = @"[离线]";
			workStatusLbl.textColor = UIColorFromRGB(0xcdcdcd);
		}
		
		UIButton *iconImgView = (UIButton *)[cell viewWithTag:118];
		if(iconImgView == nil)
		{
			UIImage *image = [UIImage imageNamed:@"ic-chakanzhuangtai_dadianhua"];
			iconImgView = [[[UIButton alloc]
							initWithFrame:CGRectMake(tableView.frame.size.width - 35 - 30,
													 (75 - 35) / 2,
													 35,
													 35)] autorelease];
			iconImgView.tag = 118;
			[iconImgView setBackgroundImage:image forState:UIControlStateNormal];
			//			iconImgView.layer.borderWidth = 1;
			[iconImgView addTarget:self action:@selector(callPhoneNumber:) forControlEvents:UIControlEventTouchUpInside];
			[cell.contentView addSubview:iconImgView];
		}
//		if(self.entity.virtualCellPhone == nil || self.entity.virtualCellPhone.length < 2)
//		{
//			iconImgView.hidden = YES;
//		}
//		else
//		{
//			iconImgView.hidden = NO;
//		}
		
		
		UILabel *nameLbl = (UILabel *)[cell viewWithTag:115];
		if(nameLbl == nil)
		{
			nameLbl = [[[UILabel alloc]
						initWithFrame:CGRectMake(headerImgView.frame.size.width + 2 * HO_HORIZONAL_PADDING,
												 HO_HORIZONAL_PADDING + 4,
												 tableView.frame.size.width - headerImgView.frame.size.width - 3 - HO_HORIZONAL_PADDING,
												 22)] autorelease];
			nameLbl.tag = 115;
			[cell.contentView addSubview:nameLbl];
			nameLbl.textColor = TEXT_COLOR_666666;
			nameLbl.font = [UIFont systemFontOfSize:15];
		}
		nameLbl.text = self.entity.userName;
		
		RatingBar *ratingBar = (RatingBar *)[cell viewWithTag:116];
		if(ratingBar == nil)
		{
			ratingBar = [[[RatingBar alloc]
									initWithFrame:CGRectMake(nameLbl.frame.origin.x,
															 nameLbl.frame.origin.y + 30,
															 90,
															 30)  isSmall:NO] autorelease];
			ratingBar.tag = 116;
			[cell.contentView addSubview:ratingBar];
		}
		ratingBar.starNumber = ((int)(self.entity.appraise + 1));
		
		UILabel *levelLbl = (UILabel *)[cell viewWithTag:117];
		if(levelLbl == nil)
		{
			levelLbl = [[[UILabel alloc]
						 initWithFrame:CGRectMake(ratingBar.frame.size.width + ratingBar.frame.origin.x + HO_HORIZONAL_PADDING * 2,
												  ratingBar.frame.origin.y,
												  100,
												  22)] autorelease];
			levelLbl.tag = 117;
			[cell.contentView addSubview:levelLbl];
			levelLbl.textColor = TEXT_COLOR_666666;
			levelLbl.font = [UIFont systemFontOfSize:15];
		}
		levelLbl.text = [NSString stringWithFormat:@"%d", ((int)(self.entity.appraise + 1))];
		return cell;
	}
	else if(indexPath.section == 1)
	{
		static NSString *HORepairerBaseInfoTableViewCellIdentifier = @"HORepairerBaseInfoTableViewCellIdentifier";
		HORepairerBaseInfoTableViewCell *cell = (HORepairerBaseInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HORepairerBaseInfoTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[HORepairerBaseInfoTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HORepairerBaseInfoTableViewCellIdentifier] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			[cell initWithWidth:baseTableview.frame.size.width];
		}
		[cell setEntity:self.entity];
		return cell;
	}
	if(indexPath.section == 2)
	{
		if(self.entity.appraiseEntity == nil)
		{
			static NSString *UIRepairerTableViewCell = @"UIRepairerTableViewCell";
			UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:UIRepairerTableViewCell];
			if(cell == nil)
			{
				cell = [[[UITableViewCell alloc]
						 initWithStyle:UITableViewCellStyleDefault
						 reuseIdentifier: UIRepairerTableViewCell] autorelease];
			}
			return cell;

		}
		static NSString *HOAppraiserTableViewCellIdentifier = @"HOAppraiserTableViewCellIdentifier";
		
		HOAppraiserTableViewCell *cell = (HOAppraiserTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOAppraiserTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[HOAppraiserTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOAppraiserTableViewCellIdentifier] autorelease];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			[cell initWithWidth:baseTableview.frame.size.width];
		}
		[cell.contentView.layer setMasksToBounds:YES];
		[cell setEntity:self.entity.appraiseEntity];
		return cell;
	}
	return nil;
}

- (void)callPhoneNumber:(id)sender
{
	[HOUtils callPhoneNumber:self.entity.virtualCellPhone];
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
#pragma mark

- (HOEditTableViewCell *)tableView:(UITableView *)tableView cellForEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOEditTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	return cell;
}
#pragma mark -- tool
- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetRepairmanInfo])
	{
		[self.entity parseDic:response.dataNode];
		if(self.entity.appraiseEntity == nil)
		{
			baseTableview.tableFooterView = [self getFooterView];
		}
		else
		{
			baseTableview.tableFooterView = nil;
		}
		[baseTableview reloadData];
	}
}

- (void)dealloc
{
	self.entity = nil;
	[super dealloc];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
